using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
//@kester
using FarseerGames.FarseerPhysics.Mathematics;

namespace FarseerGames.AdvancedSamples.DrawingSystem
{
    public class RectangleBrush
    {
        private Color _borderColor;
        private Color _color;
        private int _height;
        private float _layer;
        private Texture2D _rectangleTexture;
        private int _width = 5;

        //@kester
        private Vector2 _origin;
        private Vector2 _scale;
        private Vector2 _difference;
        private Vector2 _normalizedDifference = Vector2.Zero;
        private float _rotation;
        private float _theta;
        private Vector2 _xVector = new Vector2(1, 0);

        public RectangleBrush()
        {
            _color = Color.Black;
        }

        public RectangleBrush(int width, int height, Color color, Color borderColor)
        {
            _color = Color.Black;
            _color = color;
            _borderColor = borderColor;
            _width = width;
            _height = height;
        }

        public Color Color
        {
            get { return _color; }
            set { _color = value; }
        }

        public Color BorderColor
        {
            get { return _borderColor; }
            set { _borderColor = value; }
        }


        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public float Layer
        {
            get { return _layer; }
            set { _layer = value; }
        }


        public void Load(GraphicsDevice graphicsDevice)
        {
            _rectangleTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, _width, _height, _color,
                                                                      _borderColor);
            //@kester
            _origin = new Vector2(0, _height / 2f + 1);
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 position)
        {
            spriteBatch.Draw(_rectangleTexture, position, null, Color.White, 0,
                             new Vector2(1 + _rectangleTexture.Width / 2f, 1 + _rectangleTexture.Height / 2), 1,
                             SpriteEffects.None, _layer);
        }

        //@kester
        public void Draw(SpriteBatch spriteBatch, Vector2 startPoint, Vector2 endPoint)
        {
            Vector2.Subtract(ref endPoint, ref startPoint, out _difference);
            CalculateRotation(_difference);
            CalculateScale(_difference);
            //spriteBatch.Draw(_lineTexture, startPoint, null, _color, _rotation, _origin, _scale, SpriteEffects.None,
            //               _layer);
            spriteBatch.Draw(_rectangleTexture, startPoint, null, Color.Green, _rotation,
                            _origin, _scale,
                            SpriteEffects.None, _layer);
        }
        //@kester
        private void CalculateRotation(Vector2 difference)
        {
            Vector2.Normalize(ref difference, out _normalizedDifference);
            Vector2.Dot(ref _xVector, ref _normalizedDifference, out _theta);

            _theta = Calculator.ACos(_theta);
            if (difference.Y < 0)
            {
                _theta = -_theta;
            }
            _rotation = _theta;
        }
        //@kester
        private void CalculateScale(Vector2 difference)
        {
            float desiredLength = difference.Length();
            _scale.X = desiredLength / _rectangleTexture.Width;
            _scale.Y = 1;
        }
    }
}